# this should make a lizard walk around in some 3d matrix, collecting temperature
# the lizard walks randomly
# there is no regard for body temperature
# there is a fixed Pr(moving) = (0.5/12). this means the lizards has a 50% chance
# of moving after 2 minutes
# time steps are every 10s
# Tb is evaluated every 10s

lizard <- function(steps, pr, move)
{
  #steps is how many steps the lizard takes in an hour
  #steps=360

  #Te will keep the environmental temperatures that the lizard encounters in a nice list
  Te<-c()

  #pr is the probability that the lizard will move in some number of moves. I use 0.5 in 12 moves. i.e. the lizard has 50% chance of moving after 12 time steps
  #pr=0.5
  #move=12

  # start cell picks two random numbers, but we need to modulo this %% by the length of the axes of the Te matrix, which is held somewhere.
  start.cell<-array(c(round(runif(2,min=0,max=1000))),dim=c(1,2))
  #temp.matrix is the thermal landscape that the lizard will be walking around in
  #z<-start.cell%%c(dim(temp.matrix)[1],dim(temp.matrix)[2])
  z<-start.cell%%10

  pmove<-c()
  moore.cell<-c()
  # the lizard moves every 10 s. Stays on a plane for 1 hour = 360 steps.

  #Each row of lookup.cell is the absolute move that the lizard makes
  lookup.cell<-array(c(-1,0,1,-1,1,-1,0,1,0,1,1,1,0,0,-1,-1,-1,0),dim=c(9,2))

  for (i in 1:steps)
  {
    # does the lizard move? If pmove==1, yes. if pmove==0, no.
    pmove<-c(pmove,rbinom(1,1,(pr/move)))
    if (pmove[i]==1)
    {
      #moore.cell is the randomly chosen cell. Looked up as the row in lookup.cell
      moore.cell<-c(moore.cell,round(runif(1,min=1,max=8)))
      #z<-rbind(z,z[i]+lookup.cell[moore.cell[i],])
     
      # want to keep a record of the Te that the lizard encounters
      #Te<-c(Te,(Te[z]))
    
    }
      else
      {
        #new location = old location
        moore.cell<-c(moore.cell,9)
        #z=rbind(z,z[i,])
      }
    z<-rbind(z,z[i,]+lookup.cell[moore.cell[i],])
    #evaluate Tb based on new location
    #Tb<-c(Tb,newTb)
  }
  plot(z[,1]~z[,2],type="l")
  return(z)
}
